<script lang="ts" setup>
import { reactive, ref, getCurrentInstance } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { inject } from "vue-demi";
import { ElMessage } from "element-plus";

let { proxy } = getCurrentInstance();

const reload = inject("reload");
var fdata = new FormData();
const ruleFormRef = ref<FormInstance>();

const ruleForm = reactive({
  markers: [
    {
      position: [121.5273285, 31.21515044],
    },
    {
      position: [121.5174286, 31.21515044],
    },
  ],
  center: [121.5273285, 31.21515044],
  name: "index",
  email: "",
  zoom: 12,
  position: [121.5273285, 31.21515044],

  tel: "",

  biaoti: "",

  context: "",
});

const events = {
  init(o) {
    console.log(o.getCenter());
  },
  zoomchange: (e) => {
    console.log(e);
  },
  // zoomend: (e) => {
  //   //获取当前缩放zoom值
  //   console.log(this.$refs.map.$$getInstance().getZoom());
  //   console.log(e);
  // },
  click: (e) => {
    alert("map clicked");
  },
};

//使用其他组件
const plugin = [
  {
    pName: "Scale",
    events: {
      init(instance) {
        console.log(instance);
      },
    },
  },
  {
    pName: "ToolBar",
    events: {
      init(instance) {
        console.log(instance);
      },
    },
  },
];
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      fdata.append("email", ruleForm.email);
      fdata.append("tel", ruleForm.tel);
      fdata.append("biaoti", ruleForm.biaoti);
      fdata.append("context", ruleForm.context);
      await proxy.$axios.post("./liuyan", fdata);
      ElMessage({
        message: "发布成功",
        type: "success",
      });
      console.log("发布成功!");
      // 发布成功后清除数据
      reload();
    } else {
      ElMessage.error("发布失败");

      console.log("error submit!", fields);
    }
  });
};
</script>
<template>
  <div class="box">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
      status-icon
    >
      <div class="box1">
        <el-form-item label="您的邮箱" prop="email">
          <el-input
            class="type"
            v-model="ruleForm.email"
            placeholder="邮箱"
          ></el-input>
        </el-form-item>
        <el-form-item label="你的电话" prop="tel">
          <el-input
            class="type"
            v-model="ruleForm.tel"
            placeholder="电话"
          ></el-input>
        </el-form-item>
      </div>
      <el-form-item label="留言标题" prop="biaoti">
        <el-input
          class="type"
          v-model="ruleForm.biaoti"
          placeholder="留言标题"
        ></el-input>
      </el-form-item>
      <el-form-item label="留言描述" prop="context">
        <el-input
          class="type"
          v-model="ruleForm.context"
          placeholder="请尽量详细的声明您的需求"
          type="textarea"
        ></el-input>
      </el-form-item>
      <div class="fasong">
        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm(ruleFormRef)"
            :plain="true"
            >发送留言</el-button
          >
        </el-form-item>
      </div>
    </el-form>
  </div>
  <!-- <div class="amap-wrapper">
    <el-amap
      ref="map"
      :vid="'amapDemo'"
      :center="center"
      :zoom="zoom"
      :plugin="plugin"
      :events="events"
      class="amap-demo"
      style="height: 500px; width: 800px"
    >
      <el-amap-marker v-for="(u, i) in markers" :position="u.position" :key="i">
      </el-amap-marker>
      <el-amap-marker :position="[121.5273285, 31.21515044]" :icon="icon">
      </el-amap-marker>
    </el-amap>
  </div> -->
</template>
<style scoped>
.box {
  width: 600px;
  margin: auto;
  margin-top: 20px;
}
.box1 {
  display: flex;
}
.fasong {
  margin-left: 150px;
}
</style>
